//package com.ruoyi.system.utils.aliyun;
//
//
//import com.aliyun.ocr20191230.Client;
//import com.aliyun.ocr20191230.models.RecognizePdfRequest;
//import com.aliyun.ocr20191230.models.RecognizePdfResponse;
//import com.aliyun.teaopenapi.models.Config;
//import java.io.File;
//import java.io.FileInputStream;
//import java.io.IOException;
//import java.util.Base64;
//
//public class AliyunOcrPdfService {
//    private final String accessKeyId;
//    private final String accessKeySecret;
//    private final String regionId;
//
//    public AliyunOcrPdfService(String accessKeyId, String accessKeySecret, String regionId) {
//        this.accessKeyId = accessKeyId;
//        this.accessKeySecret = accessKeySecret;
//        this.regionId = regionId;
//    }
//
//    /**
//     * 初始化阿里云 OCR 客户端
//     * @return 客户端实例
//     * @throws Exception 异常
//     */
//    private Client createClient() throws Exception {
//        Config config = new Config()
//                .setAccessKeyId(accessKeyId)
//                .setAccessKeySecret(accessKeySecret);
//        // 访问的域名
//        config.endpoint = "ocr." + regionId + ".aliyuncs.com";
//        return new Client(config);
//    }
//
//    /**
//     * 将文件转换为 Base64 编码
//     * @param filePath 文件路径
//     * @return Base64 编码字符串
//     * @throws IOException 异常
//     */
//    private String fileToBase64(String filePath) throws IOException {
//        File file = new File(filePath);
//        try (FileInputStream fis = new FileInputStream(file)) {
//            byte[] bytes = new byte[(int) file.length()];
//            // 注意：原代码存在 'FileInputStream.read()' 结果被忽略的问题，可改为如下方式检查读取长度
//            int totalBytesRead = 0;
//            while (totalBytesRead < bytes.length) {
//                int bytesRead = fis.read(bytes, totalBytesRead, bytes.length - totalBytesRead);
//                if (bytesRead == -1) {
//                    break;
//                }
//                totalBytesRead += bytesRead;
//            }
//            return Base64.getEncoder().encodeToString(bytes);
//        }
//    }
//
//    /**
//     * 识别 PDF 文件
//     * @param pdfFilePath PDF 文件路径
//     * @return 识别结果字符串
//     * @throws Exception 异常
//     */
//    public String recognizePdf(String pdfFilePath) throws Exception {
//        Client client = createClient();
//        String pdfBase64 = fileToBase64(pdfFilePath);
//        RecognizePdfRequest request = new RecognizePdfRequest()
//                .setFileContent(pdfBase64);
//        RecognizePdfResponse response = client.recognizePdf(request);
//        return response.getBody().getData().getResult();
//    }
//}
